import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 资产库层级控制
var defaultLevels = [];

try{
    if(localStorage.levels){
        defaultLevels = JSON.parse(localStorage.getItem('levels'));
    }
}catch (e){
    console.log(e);
}

// 登录状态
var defaultLogin = {
    'uid':'',
    'username':'',
    'type':'',
    'role':'',
    'token':'',
    'isLogin':false
}

try{
    if(localStorage.login){
        defaultLogin = JSON.parse(localStorage.getItem('login'));
    }
}catch (e){
    console.log(e);
}

export default new Vuex.Store({
    state:{
        levels:defaultLevels,
        login:defaultLogin
    },
    mutations:{
        // 改变导航层级
        nextLevel(state,fid){
            state.levels.push(fid);
            localStorage.setItem('levels',JSON.stringify(state.levels));

            // console.log(localStorage.levels);
        },
        lastLevel(state){
            state.levels.pop();
            localStorage.setItem('levels',JSON.stringify(state.levels));
            // console.log(localStorage.levels);
        },
        restoreLevel(state){
            state.levels = [];
            localStorage.setItem('levels',JSON.stringify(state.levels));
            //console.log(localStorage.levels);
        },

        // 登录令牌
        storeToken(state,data){
            state.login = data;
            localStorage.setItem('login',JSON.stringify(state.login));
        },
        // 注销清除令牌
        clearToken(state){
            let current = {
                'uid':'',
                'username':'',
                'type':'',
                'role':'',
                'token':'',
                'isLogin':false
            };
            state.login = current;
            localStorage.setItem('login',JSON.stringify(state.login));
        }
    }
})
